package com.qiang.test16;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Test05 {
    /* public static void main(String[] args) {
         Thread thread = new Thread(new Runnable() {
             @Override
             public void run() {
                 for (int i = 0; i < 5; i++) {
                     System.out.println(i);
                 }
             }
         });
         thread.start();
     }*/
    public static void main(String[] args) {

        /**
         * 核心线程数量为1 ， 最大线程池数量为3, 任务容器的容量为1 ,空闲线程的最大存在时间为20s
         */
        ThreadPoolExecutor tt = new ThreadPoolExecutor(
                1,
                3,
                20,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(1),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());

        // 提交5个任务，而该线程池最多可以处理4个任务，当我们使用AbortPolicy这个任务处理策略的时候，就会抛出异常
        /*for (int x = 0; x < 5; x++) {
            tt.submit(() -> {
                System.out.println(Thread.currentThread().getName() + "---->> 执行了任务");
            });
        }*/

        /*for (int i = 0; i < 5; i++) {
            Thread thread = new Thread() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName() + "---->> 执行了任务");
                }
            };
            tt.submit(thread);
        }*/

       /* for (int i = 0; i < 5; i++) {
            Runnable rr = new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName() + "---->> 执行了任务");
                }
            };
            tt.submit(rr);
        }*/


        tt.shutdown();

    }
}
